package com.walleipt.corgi.api.controller;

import java.util.List;
import java.util.Map;

import org.mangoframework.core.annotation.Post;
import org.mangoframework.core.annotation.RequestMapping;
import org.mangoframework.core.dispatcher.Parameter;

import com.walle.ak47.commons.utils.ConvertUtils;
import com.walleipt.corgi.api.annotations.Ignore;
import com.walleipt.corgi.api.common.dao.mysql.SmartDao;
import com.walleipt.corgi.api.ex.Result;
import com.walleipt.corgi.api.ex._HashMap;
import com.walleipt.corgi.api.utils.AssertUtils;
import com.walleipt.corgi.api.utils.MemoryUtils;
import com.walleipt.corgi.api.utils.StringUtils;

@RequestMapping("/agentLogin")
public class AgentLoginController {

	String tableName = "agent_register_user" ;
	
    @Ignore
    @Post()
    public Object login(Parameter parameter) {
    	
        String mobile = parameter.getParamString().get("mobile");
        String password = parameter.getParamString().get("password");
        AssertUtils.notNull(mobile, "手机号码为空");
        AssertUtils.notNull(password, "密码为空");
        password = StringUtils.password(password);
        
        List<Map<String,Object>> user  = SmartDao.instance().find( "select * from "+tableName+" where mobile=? and password=?", mobile, password ) ;
        AssertUtils.isTrue((user!=null && user.size()==1), "用户名或密码错误");
        if ( 1 == ConvertUtils.toInt(user.get(0).get("flag").toString()) ) {
            return new Result(Result.ERROR, "invalid status");
        }

        String token = MemoryUtils.token();
        MemoryUtils.put(token, 	"user", user); 
        parameter.getRequest().getSession().setAttribute("user",user);
        return new _HashMap<>().add("token", token).add("user", user);
    }

}
